<?php
/**
 Copyright (C) 2009 Attila-Mihaly Balazs

 TwitFeeder is free software: you can redistribute it and/or modify it
 under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 TwitFeeder is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.

 You should have received a copy of the GNU Affero General Public License
 along with TwitFeeder.  If not, see <http://www.gnu.org/licenses/>
 **/
error_reporting(0);
require 'privatedata.php';

if ($AUTH != @$_SERVER['HTTP_X_SHARED_SECRET']) {
    header("HTTP/1.0 404 Not Found");
    exit;
}

$url = @$_GET['q'];
if (!isset($url)) {
    header("HTTP/1.0 404 Not Found");
    exit;
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);

if (isset($_SERVER['HTTP_USER_AGENT'])) {
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
}
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
    curl_setopt($ch, CURLOPT_USERPWD, "{$_SERVER['PHP_AUTH_USER']}:{$_SERVER['PHP_AUTH_PW']}");
}

if (count($_POST) > 0) {
    curl_setopt($ch, CURLOPT_POST, true);
    $postfields = array();
    foreach ($_POST as $key => $val) {
        $postfields[] = urlencode($key) . '=' . urlencode($val);
    }
    curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $postfields));
}

$headers = array();
function header_callback($ch, $header) {
    global $headers;
    // we add our own content encoding
    // also, the content length might vary because of this
    if (false === stripos($header, 'Content-Encoding: ')
        && false === stripos($header, 'Content-Length: ')
        && false === stripos($header, 'Transfer-Encoding: ')) {
        $headers[] = $header;
    }
    return strlen($header);
}

curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'header_callback');

$output = curl_exec($ch);
if (FALSE === $output) {
    header("HTTP/1.0 500 Server Error");
    print curl_error($ch);
    exit;
}
curl_close($ch);

foreach ($headers as $header) {
    header($header);
}
print $output;